<template>
  <div class="app-container">
    <el-card class="box-card" :body-style="{ padding: '24px' }">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="用户名称">
          <el-input v-model="listQuery.userName" clearable placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="角色名称">
          <el-input v-model="listQuery.roleName" clearable placeholder="请输入角色名称" />
        </el-form-item>
        <el-button type="primary" @click="handleFilter">查询</el-button>
        <el-button type="primary" @click="handleCreate">新增</el-button>
        <el-button type="danger" @click="deleteUserRole">删除</el-button>
      </el-form>

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="userRoleList"
        header-cell-class-name="table-header-cell"
        fit
        highlight-current-row
        border
        style="width: 100%;"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55px" />
        <el-table-column prop="sId" label="序号" type="index" width="50px" align="center" />
        <el-table-column v-if="false" prop="id" label="ID" align="center" />
        <el-table-column v-if="false" prop="userId" label="用户ID" align="center" />
        <el-table-column prop="userName" label="用户名称" align="center" />
        <el-table-column v-if="false" prop="roleId" label="角色ID" align="center" />
        <el-table-column prop="roleName" label="角色名称" align="center" />
      </el-table>
      <pagination v-show="listTotal>0" :total="listTotal" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
    </el-card>
    <el-dialog :title="textMap['create']" :visible.sync="dialogFormVisible" :body-style="{ 'min-height': 'calc(100vh - 150px)',padding: '24px' }" width="35%" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="dataForm" label-position="left" label-width="90px" style="width: 80%; margin-left:50px;">
        <el-form-item label="角色">
          <el-select v-model="role" clearable placeholder="请选择类型">
            <el-option v-for="item in roleData" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-table ref="userDataTable" v-loading="userLoading" :data="userData" :row-key="rowKey" fixed header-cell-class-name="table-header-cell" border min-height="900px" @selection-change="handleUserSelectionChange">
          <el-table-column type="selection" width="55px" :reserve-selection="true" />
          <el-table-column prop="sId" label="序号" type="index" width="50px" align="center" />
          <el-table-column v-if="false" prop="id" label="ID" align="center" />
          <el-table-column prop="userName" label="用户名称" align="center" />
          <el-table-column prop="loginName" label="登录名称" align="center" />
        </el-table>
        <pagination v-show="userTotal>0" :total="userTotal" :page.sync="userQuery.current" :limit.sync="userQuery.size" @pagination="getUserList" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createData">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { fetchUserRoles, deleteUserRoles, addUserRole } from '@/api/user-role.js'
import { fetchUsers } from '@/api/user.js'
import { findRoles } from '@/api/role.js'
export default {
  name: 'UserRoleManagement',
  components: { Pagination },
  filters: {},
  data() {
    return {
      role: '',
      userRoleList: null,
      dialogFormVisible: false,
      listLoading: true,
      userLoading: true,
      multipleSelection: [],
      userData: [],
      userValues: [],
      listTotal: 1,
      listQuery: {
        current: 1,
        size: 10
      },
      userTotal: 0,
      userQuery: {
        current: 1,
        size: 10
      },
      userRoleTemp: {
        userIds: '',
        roleIds: ''
      },
      textMap: {
        update: '修改',
        create: '新增'
      },
      roleData: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchUserRoles(this.listQuery).then(response => {
        const res = response.retBody
        this.userRoleList = res.records
        this.listTotal = res.total
      })
      this.listLoading = false
    },
    getUserList() {
      const arr = []
      this.userLoading = true
      fetchUsers(this.userQuery).then(response => {
        const res = response.retBody
        const userList = res.records
        this.userTotal = res.total
        userList.forEach(item => {
          arr.push({
            id: item.id,
            userName: item.userName,
            loginName: item.loginName
          })
        })
      })
      this.userData = arr
      this.userLoading = false
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleUserSelectionChange(val) {
      this.userValues = val
    },
    deleteUserRole() {
      if (!this.multipleSelection.length) {
        this.$alert('请选择要删除的数据', '警告')
        return null
      }
      const arr = []
      this.multipleSelection.forEach(row => {
        arr.push(row.id)
      })
      const ids = arr.join(',')
      this.$confirm('确定要删除该条用户信息?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserRoles(ids).then(() => {
          this.$notify({
            title: '成功',
            message: '删除信息成功！',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handleCreate() {
      this.userValues = []
      findRoles().then(response => {
        const roleList = response.retBody
        this.roleData = []
        roleList.forEach(item => {
          this.roleData.push({
            value: item.id,
            label: item.roleName
          })
        })
      })
      this.getUserList()
      this.dialogFormVisible = true
    },
    createData() {
      const arr = []
      this.userValues.forEach(item => {
        arr.push(item.id)
      })
      if (!this.role) {
        this.$message({
          message: '请选择要添加的角色',
          type: 'warning'
        })
        return
      }
      if (!arr.length) {
        this.$message({
          message: '请选择用户',
          type: 'warning'
        })
        return
      }
      this.dialogFormVisible = false
      const data = {
        roleId: this.role,
        userIds: arr.join(',')
      }
      addUserRole(data).then(response => {
        this.$notify({
          title: '成功',
          message: '添加成功！',
          type: 'success',
          duration: 1000
        })
        this.role = ''
        this.$refs.userDataTable.clearSelection()
        this.getList()
      })
    },
    rowKey(row) {
      return row.id
    }
  }
}
</script>
<style scoped>

</style>
